/*
 * (C) 2012	Sheng Yi
 * Use exec calls : after fork, we execute new program /root/trunk/apue/echoall.
 */
#include "apue.h"
#include <sys/wait.h>

char *envp[] = {"PATH=/root/trunk/apue/", "USER=unkown", NULL}; /* my environment table */
char *argv[] = {"Hello", "ShengYi", "WELCOME", "BYE", "BYE", NULL}; /* my arguments table */
char pathname[] = "/root/trunk/apue/echoall";	/* pahtname */
char filename[] = "echoall";	/* filename */

int main()
{
	pid_t pid;

//	if ((pid = fork())< 0)
//		err_quit("fork error");
//	else if (pid == 0) /* child */
//	{
//		/* execl---use default environment table */
//		if (execl(pathname, argv[0], argv[1], argv[2], argv[3], argv[4], (char*)0) < 0)
//			err_quit("execl error");
//	}
//	/* wait for the child termination */
//	if (waitpid(pid, NULL, 0) != pid)
//		err_quit("waitpid error");

//	if ((pid = fork())< 0)
//		err_quit("fork error");
//	else if (pid == 0) /* child */
//	{
//		/* execv---use default environment table */
//		if (execv(pathname, argv) < 0)
//			err_quit("execv error");
//	}
//	/* wait for the child termination */
//	if (waitpid(pid, NULL, 0) != pid)
//		err_quit("waitpid error");

//	if ((pid = fork())< 0)
//		err_quit("fork error");
//	else if (pid == 0) /* child */
//	{
//		/* execle---use my environment table  */
//		if (execle(pathname, argv[0], argv[1], argv[2], argv[3], argv[4], (char*)0, envp) < 0)
//			err_quit("execle error");
//	}
//	/* wait for the child termination */
//	if (waitpid(pid, NULL, 0) != pid)
//		err_quit("waitpid error");

	if ((pid = fork())< 0)
		err_quit("fork error");
	else if (pid == 0) /* child */
	{
		/* execve---use my environment table */
		if (execve(pathname, argv, envp) < 0)
			err_quit("execve error");
	}
	/* wait for the child termination */
	if (waitpid(pid, NULL, 0) != pid)
		err_quit("waitpid error");

//	if ((pid = fork())< 0)
//		err_quit("fork error");
//	else if (pid == 0) /* child */
//	{
//		/* execlp---use filename and default */
//		if (execlp(filename, argv[0], argv[1], argv[2], argv[3], argv[4], (char*)0) < 0)
//			err_quit("execlp error");
//	}
//	/* wait for the child termination */
//	if (waitpid(pid, NULL, 0) != pid)
//		err_quit("waitpid error");

//	if ((pid = fork())< 0)
//		err_quit("fork error");
//	else if (pid == 0) /* child */
//	{
//		/* execvp */
//		if (execvp(filename, argv) < 0)
//			err_quit("execvp error");
//	}
//	/* wait for the child termination */
//	if (waitpid(pid, NULL, 0) != pid)
//		err_quit("waitpid error");

	exit(0);
}

